package com.yl.applicationsecurity.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.pojo.*;
import com.yl.applicationsecurity.service.ScriptStateService;
import com.yl.applicationsecurity.service.SgmOtherVulnerabilityService;
import com.yl.applicationsecurity.service.SgmSafeRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.List;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-03-06 14:52:55
 */
@RestController
@RequestMapping("/sgmOtherVulnerability")
public class SgmOtherVulnerabilityController {


    @Autowired
    private SgmOtherVulnerabilityService sgmOtherVulnerabilityService;


    @Autowired
    private ScriptStateService scriptStateService;


    @PostMapping("/list")
    public Result list(String[] nationSelect, String[] systemNameSelect, SgmOtherVulnerability sgmOtherVulnerability, @RequestParam(required = false) Date[] startAndEndDate, @RequestParam(defaultValue = "1", value = "pageNum")Integer pageNum
            , @RequestParam(defaultValue = "10", value = "pageSize")Integer pageSize ){
        PageInfo<SgmOtherVulnerability> sgmSafeRiskList = null;
        if(startAndEndDate!=null&&startAndEndDate.length!=2){
            sgmSafeRiskList =  sgmOtherVulnerabilityService.findListBySgmOtherVulnerability(nationSelect,systemNameSelect,sgmOtherVulnerability,null,null,pageNum,pageSize);
        }else{
            sgmSafeRiskList =  sgmOtherVulnerabilityService.findListBySgmOtherVulnerability(nationSelect,systemNameSelect,sgmOtherVulnerability,startAndEndDate[0],startAndEndDate[1],pageNum,pageSize);
        }

        return Result.success(sgmSafeRiskList);
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(HttpServletRequest request, String[]  ids, Integer updateStatus) {

        if (sgmOtherVulnerabilityService.updateStatus(ids, updateStatus) != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }


    @RequestMapping("/insert")
    public Result insert(@RequestBody List<SgmOtherVulnerabilityInsert> list) {

        scriptStateService.updateDate("交互式逻辑探针-其他");
        //  时间转换String dateTimeStr = "2025-02-19 14:30:00";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;

        SgmOtherVulnerability sgmOtherVulnerability = new SgmOtherVulnerability();
        for (SgmOtherVulnerabilityInsert sgmOtherVulnerabilityInsert : list) {
            if (StringUtils.isEmpty(sgmOtherVulnerabilityInsert.getUrl()) || StringUtils.isEmpty(sgmOtherVulnerabilityInsert.getRequestText()) || StringUtils.isEmpty(sgmOtherVulnerabilityInsert.getResponseText()) || sgmOtherVulnerabilityInsert.getRiskType() == null || StringUtils.isEmpty(sgmOtherVulnerabilityInsert.getCreateDate())) {
                continue;
            }

            // request base64解码
            byte[] decodedBytes = Base64.getDecoder().decode(sgmOtherVulnerabilityInsert.getRequestText());
            sgmOtherVulnerabilityInsert.setRequestText(new String(decodedBytes));

            // response base64解码
            decodedBytes = Base64.getDecoder().decode(sgmOtherVulnerabilityInsert.getResponseText());
            sgmOtherVulnerabilityInsert.setResponseText(new String(decodedBytes));

            try {
                date = formatter.parse(sgmOtherVulnerabilityInsert.getCreateDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            sgmOtherVulnerability  = new SgmOtherVulnerability();
            sgmOtherVulnerability.setCreateDate(date);
            sgmOtherVulnerability.setCountry(sgmOtherVulnerabilityInsert.getCountry());
            sgmOtherVulnerability.setRiskType(sgmOtherVulnerabilityInsert.getRiskType());
            sgmOtherVulnerability.setRequestText(sgmOtherVulnerabilityInsert.getRequestText());
            sgmOtherVulnerability.setResponseText(sgmOtherVulnerabilityInsert.getResponseText());
            sgmOtherVulnerability.setUrl(sgmOtherVulnerabilityInsert.getUrl());
            sgmOtherVulnerabilityService.add(sgmOtherVulnerability);
        }
        return Result.success("添加成功");
    }


}

